package bigdata.api.login;

import bigdata.webbase.domain.SessionParamEnum;
import framework.util.EncryptUtil;
import framework.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pub.user.domain.Account;
import pub.user.domain.LoginResult;
import pub.user.domain.User;
import pub.user.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述:
 *
 * @author 002465
 * @created 2017/5/16 19:38
 * @return ${return_type}
 * @since v1.0.0
 * ${tags}
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/verification")
    @ResponseBody
    public String verification(ModelMap modelMap, HttpServletRequest request,Account account,String callBack) {
        LoginResult loginResult = new LoginResult();
        loginResult.setSuccess(true);
        Account sessionAccount = (Account) request.getSession().getAttribute(
                SessionParamEnum.Account.getValue());
        if(account == null){
            loginResult.setSuccess(false);
            loginResult.setMsg("账户不能为空!");
            return LoginConvert.toJson(loginResult,callBack);
        }
        String loginName = account.getLoginName();
        if(StringUtils.isBlank(loginName)){
            loginResult.setSuccess(false);
            loginResult.setMsg("登陆名不能为空!");
            return LoginConvert.toJson(loginResult,callBack);
        }
        String passWord = account.getPassWord();
        if(StringUtils.isBlank(passWord)){
            loginResult.setSuccess(false);
            loginResult.setMsg("密码不能为空!");
            return LoginConvert.toJson(loginResult,callBack);
        }
        User entityUser = userService.getUserByLoginName(loginName);
        if(entityUser == null){
            loginResult.setSuccess(false);
            loginResult.setMsg("账户不存在!");
            return LoginConvert.toJson(loginResult,callBack);
        }
        account.encryptPass();
        if(!account.getPassWord().equals(entityUser.getAccount().getPassWord())){
            loginResult.setSuccess(false);
            loginResult.setMsg("密码错误!");
            return LoginConvert.toJson(loginResult,callBack);
        }
        request.getSession().setAttribute(SessionParamEnum.Account.getValue(),account);
        return LoginConvert.toJson(loginResult,callBack);
    }
}
